﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using ControllerClient.UserTool;

namespace ControlMachine
{
     public class SignHelper
    {
         public static SignatureInfo getSignatureInfo(string SignDataHexString)
         {
             SignatureInfo SignatureInfo = new SignatureInfo();
             string CaData = "";
             Device device = null;
             try
             {
                 device = new Device();
                 //获取读卡器列表
                 List<string> readers = device.iniDevice();
                 foreach (string reader in readers)
                 {
                     Console.Out.WriteLine("读卡器名称=" + reader);
                 }
                 //初始化卡片
                 device.iniCPUCard(readers);
                 byte[] sw12 = new byte[2];
                 byte[] retMessage = new byte[512];
                 //发送APDU指令
                 //     retMessage = device.sendAPDU("00A4040008A00000033301010100", sw12);
                 retMessage = device.sendAPDU("8010020000", sw12);
                 CaData += StringAndBytesHelper.ToHexString(retMessage, retMessage.Length);
                 //Console.Out.WriteLine("retMessag=" + StringAndBytesHelper.ToHexString(retMessage, retMessage.Length));
                 //Console.Out.WriteLine("长度=" + retMessage.Length);
                 //Console.Out.WriteLine("sw12=" + StringAndBytesHelper.ToHexString(sw12, sw12.Length));

                 retMessage = device.sendAPDU("8010020162", sw12);
                 CaData += StringAndBytesHelper.ToHexString(retMessage, retMessage.Length);
               //  Console.Out.WriteLine("CaData=" + CaData);
                // Console.Out.WriteLine("长度=" + CaData.Length);
               //  Console.Out.WriteLine("sw12=" + StringAndBytesHelper.ToHexString(sw12, sw12.Length));

                 SignatureInfo.Cadata = CaData;
                 //5053414D49303031 用户ID
                 //4D494D49535F5053414D5F55534552 PIN码
                 string userdate = "5053414D493030314D494D49535F5053414D5F55534552";
                 string checkusercmd = "80200000" + Convert.ToString(StringAndBytesHelper.strToToHexByte(userdate).Length, 16) + userdate;
               //  Console.Out.WriteLine("checkusercmd=" + checkusercmd);
                 retMessage = device.sendAPDU(checkusercmd, sw12);
               //  Console.Out.WriteLine("sw12=" + StringAndBytesHelper.ToHexString(sw12, sw12.Length));

                 string signdata = SignDataHexString;
                 byte[] signdataB = SHA1.Create().ComputeHash(StringAndBytesHelper.strToToHexByte(SignDataHexString));
                 string signdatacmd = "80320000" + Convert.ToString(signdataB.Length, 16) + StringAndBytesHelper.ToHexString(signdataB, signdataB.Length) + "80";
              //   Console.Out.WriteLine("signdatacmd=" + signdatacmd);


                 retMessage = device.sendAPDU(signdatacmd, sw12);
               //  Console.Out.WriteLine("retMessage=" + StringAndBytesHelper.ToHexString(retMessage, retMessage.Length));
               //  Console.Out.WriteLine("长度=" + retMessage.Length);
               //  Console.Out.WriteLine("sw12=" + StringAndBytesHelper.ToHexString(sw12, sw12.Length));

                 signdatacmd = "00C0000080";
                 retMessage = device.sendAPDU(signdatacmd, sw12);
               //  Console.Out.WriteLine("signdata=" + StringAndBytesHelper.ToHexString(retMessage, retMessage.Length));
               //  Console.Out.WriteLine("长度=" + retMessage.Length);
                // Console.Out.WriteLine("sw12=" + StringAndBytesHelper.ToHexString(sw12, sw12.Length));
                 SignatureInfo.Sighdata = StringAndBytesHelper.ToHexString(retMessage, retMessage.Length);

                 return SignatureInfo;
             }
             catch (Exception e)
             {
                 Console.Out.Write(e.StackTrace);
                 return null;
             }
             finally
             {
                 if (device != null)
                 {
                     device.closeDevice();
                 }
             }
         }
    }
}
